/*--------------------------------------------------------------------------
 *
 * 	You may freely copy, distribute, and reuse the code in this example.
 * 	SHL Systemhouse disclaims any warranty of any kind, expressed or  
 *	implied, as to its fitness for any particular use.
 *
 *
 *	DatabaseLevel
 *
 *	Inherits From:		NSObject
 *
 *	Conforms To:		None
 *
 *	Declared In:		DatabaseLevel.h
 *
 *------------------------------------------------------------------------*/
#import "DatabaseLevel.h"
#import "ScrollViewExtensions.h"
#import <eoaccess/eoaccess.h>
#import <appkit/appkit.h>



@implementation DatabaseLevel

- connect: sender
{
	id	path;
	id	adaptor;
	
	[messageConsole clear: nil];
	[messageConsole sprintf: "connecting to database...\n"];

	[messageConsole sprintf: "obtaining model path...\n"];
	if ((path = [EOModel findPathForModelNamed:
		[NSString stringWithCString:"Authors"]]) == nil)
		{
		NXRunAlertPanel (NULL, "Unable to obtain model path", 
			NULL, NULL, NULL);
		[messageConsole sprintf: "unable to obtain model path\n"];
		return nil;
		}

	[modelPath setStringValue: [path cString]];
		
	[messageConsole sprintf: "initializing model...\n"];
	if ((model = [[EOModel alloc] initWithContentsOfFile:path]) == nil)
		{
		NXRunAlertPanel (NULL, "Unable to initialize model", NULL, NULL, NULL);
		[messageConsole sprintf: "unable to initialize model\n"];
		return nil;
		}
	
	[messageConsole sprintf: "creating database...\n"];
	if ((database = [[EODatabase alloc] initWithModel:model]) == nil)
		{
		NXRunAlertPanel (NULL, "Unable to create database", NULL, NULL, NULL);
		[messageConsole sprintf: "unable to create database\n"];
		return nil;
		}
	
	[messageConsole sprintf: "creating context...\n"];
	if ((context = [[EODatabaseContext alloc] 
		initWithDatabase:database]) == nil)
		{
		NXRunAlertPanel (NULL, "Unable to create context", NULL, NULL, NULL);
		[messageConsole sprintf: "unable to create context\n"];
		return nil;
		}
		
	[messageConsole sprintf: "creating channel...\n"];
	if ((channel = [[EODatabaseChannel alloc] 
		initWithDatabaseContext:context]) == nil)
		{
		NXRunAlertPanel (NULL, "Unable to create channel", NULL, NULL, NULL);
		[messageConsole sprintf: "unable to create channel\n"];
		return nil;
		}
		
	// Bring up a login panel and get the valid connection dictionary
	adaptor = [database adaptor];
 	if(![adaptor runLoginPanelAndValidateConnectionDictionary]) {
			[messageConsole sprintf: "invalid login\n"]; 
			return nil;
		}
	else { 
   	    [adaptor setConnectionDictionary: [adaptor connectionDictionary]];
		[messageConsole sprintf: "opening channel...\n"];
		if ([channel openChannel] == NO)
		{
		NXRunAlertPanel (NULL, "Unable to open channel", 
														NULL, NULL, NULL);
		[messageConsole sprintf: "unable to open channel\n"];
		return nil;
		}
	}	
	[messageConsole sprintf:"channel %s open\n\n", 
		[channel isOpen] ? "is" : "is not"];
	[connectButton setEnabled: ! [channel isOpen]];
	[selectButton setEnabled: [channel isOpen]];
	return self;
}


- select: sender
{
	int	count = 0;
	id	entity, qualifier, object;
	
	[resultConsole clear: nil];
	[messageConsole sprintf: "beginning transaction...\n"];

	if ([context beginTransaction] == NO)
		{
		NXRunAlertPanel (NULL, "Unable to begin transaction", 
			NULL, NULL, NULL);
		[messageConsole sprintf: "unable to begin transaction\n"];
		return nil;
		}
		
	[messageConsole sprintf: "obtaining entity...\n"];
	if ((entity = [model entityNamed: @"authors"]) == nil)
		{
		NXRunAlertPanel (NULL, "Unable to obtain entity authors", 
			NULL, NULL, NULL);
		[messageConsole sprintf: "unable to obtain entity authors\n"];
		return nil;
		}
		
	[messageConsole sprintf: "obtaining qualifier...\n"];
	if ((qualifier = [entity qualifier]) == nil)
		{
		NXRunAlertPanel (NULL, "Unable to obtain qualifier", 
			NULL, NULL, NULL);
		[messageConsole sprintf: "unable to obtain qualifier\n"];
		return nil;
		}
		
	[messageConsole sprintf: "selecting objects...\n"];
	if ([channel selectObjectsDescribedByQualifier:qualifier
		fetchOrder:nil] == NO)
		{
		NXRunAlertPanel (NULL, "Unable to select objects", 
			NULL, NULL, NULL);
		[messageConsole sprintf: "unable to select objects\n"];
		return nil;
		}
		
	while ((object = [channel fetchWithZone:[(EODatabase*)database zone]]) != nil)
		{
		[resultConsole sprintf: "%s\n", 
			[[(NSString*)object description] cString]];
		count++;
		}
		
	[messageConsole sprintf: "\n%d records found\n\n", count];
		
	[messageConsole sprintf: "rolling back transaction...\n"];
	if ([context rollbackTransaction] == NO)
		{
		NXRunAlertPanel (NULL, "Unable to rollback transaction", 
			NULL, NULL, NULL);
		[messageConsole sprintf: "unable to rollback transaction\n"];
		return nil;
		}
		
	[messageConsole sprintf: "done\n\n"];
	return self;
}


@end
	